package com.tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil {
	public Properties properties;
	public String filename="config.properties";
	public PropertiesUtil(){
		config("");
	}
	public PropertiesUtil(String filename){
		config(filename);
	}
	public PropertiesUtil(String basepath,String filename){
		config(basepath,filename);
	}
	public void config(String basepath,String filename){
		
		if(basepath==null||"".equals(basepath))
			basepath=System.getProperty("user.dir");
		if(filename==null||"".equals(filename))
			filename=this.filename;
		filename=basepath+filename;
		//System.out.println("==========="+filename);
		try {
			File file=new File(filename);
			if(file.exists())
				properties=new Properties();
			FileInputStream input=new FileInputStream(filename);
			properties.load(input);
			input.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void config(String filename){
		
		if(filename==null||"".equals(filename)){
			filename=this.filename;
		String basepath=System.getProperty("user.dir");
		filename=basepath+"\\WebRoot\\WEB-INF\\"+filename;
		}
		try {
			File file=new File(filename);
			if(file.exists())
				properties=new Properties();
			FileInputStream input=new FileInputStream(filename);
			properties.load(input);
			input.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public String getItemValue(String itemname){
		return properties.getProperty(itemname);
		
	}
	public void setItemValue(String itemname,String value){
		properties.setProperty(itemname, value);
		
	}
	public void list(){
		properties.list(System.out);
	}
	public void print(String mes){
		
		//System.out.println("==="+mes);
	}
	public static void main(String[] args){
		PropertiesUtil p=new PropertiesUtil();
		p.list();
		
		
	}

}
